package pay.service.impl;

import cn.hutool.core.util.StrUtil;
import com.common.domain.MyException;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import pay.enums.CommonStatusEnum;
import pay.enums.ErrorCodeConstants;
import pay.enums.PayOrderStatusEnum;
import pay.mapper.PayAppMapper;
import pay.mapper.PayOrderMapper;
import pay.module.channel.PayAppDO;
import pay.service.PayAppService;

import javax.annotation.Resource;
import java.util.*;

/**
 * 支付应用信息 Service 实现类
 *
 * @author aquan
 */
@Service
@Validated
public class PayAppServiceImpl implements PayAppService {

    @Resource
    private PayAppMapper appMapper;
//    @Resource
//    private PayMerchantMapper merchantMapper;
    @Resource
    private PayOrderMapper orderMapper;
//    @Resource
//    private PayRefundMapper refundMapper;

//    @Override
//    public Long createApp(PayAppCreateReqVO createReqVO) {
//        // 插入
//        PayAppDO app = PayAppConvert.INSTANCE.convert(createReqVO);
//        appMapper.insert(app);
//        // 返回
//        return app.getId();
//    }

//    @Override
//    public void updateApp(PayAppUpdateReqVO updateReqVO) {
//        // 校验存在
//        this.validateAppExists(updateReqVO.getId());
//        // 更新
//        PayAppDO updateObj = PayAppConvert.INSTANCE.convert(updateReqVO);
//        appMapper.updateById(updateObj);
//    }

    @Override
    public void deleteApp(String id) {
        // 校验存在
        this.validateAppExists(id);
        this.validateOrderTransactionExist(id);

        // 删除
        appMapper.deleteById(id);
    }

    private void validateAppExists(String id) {
        if (appMapper.selectById(id) == null) {
            throw new MyException(ErrorCodeConstants.PAY_APP_NOT_FOUND.getCode(),ErrorCodeConstants.PAY_APP_NOT_FOUND.getMsg());
        }
    }

    @Override
    public PayAppDO getApp(String id) {
        return appMapper.selectById(id);
    }

    @Override
    public List<PayAppDO> getAppList(Collection<String> ids) {
        return appMapper.selectBatchIds(ids);
    }

//    @Override
//    public PageResult<PayAppDO> getAppPage(PayAppPageReqVO pageReqVO) {
//        Set<String> merchantIdList = this.getMerchantCondition(pageReqVO.getMerchantName());
//        if (StrUtil.isNotBlank(pageReqVO.getMerchantName()) && CollectionUtil.isEmpty(merchantIdList)) {
//            return new PageResult<>();
//        }
//        return appMapper.selectPage(pageReqVO, merchantIdList);
//    }
//
//    @Override
//    public List<PayAppDO> getAppList(PayAppExportReqVO exportReqVO) {
//        Set<String> merchantIdList = this.getMerchantCondition(exportReqVO.getMerchantName());
//        if (StrUtil.isNotBlank(exportReqVO.getMerchantName()) && CollectionUtil.isEmpty(merchantIdList)) {
//            return new ArrayList<>();
//        }
//        return appMapper.selectList(exportReqVO, merchantIdList);
//    }

    /**
     * 获取商户编号集合，根据商户名称模糊查询得到所有的商户编号集合
     *
     * @param merchantName 商户名称
     * @return 商户编号集合
     */
    private Set<String> getMerchantCondition(String merchantName) {
        if (StrUtil.isBlank(merchantName)) {
            return Collections.emptySet();
        }
        HashSet<String> set = new HashSet();
        set.add("1700902783L");
        return set;
    }

    @Override
    public void updateAppStatus(String id, Integer status) {
        // 校验商户存在
        this.checkAppExists(id);
        // 更新状态
        PayAppDO app = new PayAppDO();
        app.setId(id);
        app.setStatus(status);
        appMapper.updateById(app);
    }

    @Override
    public List<PayAppDO> getListByMerchantId(String merchantId) {
        return appMapper.getListByMerchantId(merchantId);
    }

    /**
     * 检查商户是否存在
     *
     * @param id 商户编号
     */
    @VisibleForTesting
    public void checkAppExists(String id) {
        if (id == null) {
            return;
        }
        PayAppDO payApp = appMapper.selectById(id);
        if (payApp == null) {
            throw new MyException(ErrorCodeConstants.PAY_APP_NOT_FOUND.getCode(),ErrorCodeConstants.PAY_APP_NOT_FOUND.getMsg());
        }
    }

    /**
     * 验证是否存在交易中或者退款中等处理中状态的订单
     *
     * @param appId 应用 ID
     */
    private void validateOrderTransactionExist(String appId) {
        // 查看交易订单
        if (orderMapper.selectCount(appId, PayOrderStatusEnum.WAITING.getStatus()) > 0) {
            throw new MyException(ErrorCodeConstants.PAY_APP_EXIST_TRANSACTION_ORDER_CANT_DELETE.getCode(),ErrorCodeConstants.PAY_APP_EXIST_TRANSACTION_ORDER_CANT_DELETE.getMsg());
        }
        // 查看退款订单
//        if (refundMapper.selectCount(appId, PayRefundStatusEnum.CREATE.getStatus()) > 0) {
//            throw new MyException(ErrorCodeConstants.PAY_APP_EXIST_TRANSACTION_ORDER_CANT_DELETE.getCode(),ErrorCodeConstants.PAY_APP_EXIST_TRANSACTION_ORDER_CANT_DELETE.getMsg());
//        }
    }

    @Override
    public PayAppDO validPayApp(String id) {
        PayAppDO app = appMapper.selectById(id);
        // 校验是否存在
        if (app == null) {
            throw new MyException(ErrorCodeConstants.PAY_APP_NOT_FOUND.getCode(),ErrorCodeConstants.PAY_APP_NOT_FOUND.getMsg());
        }
        // 校验是否禁用
        if (CommonStatusEnum.DISABLE.getStatus().equals(app.getStatus())) {
            throw new MyException(ErrorCodeConstants.PAY_APP_IS_DISABLE.getCode(),ErrorCodeConstants.PAY_APP_IS_DISABLE.getMsg());
        }
        return app;
    }

}
